import 'dart:developer' as developer;

class Word {
  final int? id;
  final String word;
  final String? translation;
  final String? pronunciation; // 添加pronunciation字段
  final String? meaning; // 添加meaning字段
  final String? example;
  final String? note;
  final String? category;
  final int difficulty;
  final DateTime createdAt;
  final DateTime? lastReviewedAt;
  final int memoryStrength;

  Word({
    this.id,
    required this.word,
    this.translation,
    this.pronunciation, // 添加pronunciation参数
    this.meaning, // 添加meaning参数
    this.example,
    this.note,
    this.category,
    this.difficulty = 1,
    DateTime? createdAt,
    this.lastReviewedAt,
    this.memoryStrength = 0,
  }) : createdAt = createdAt ?? DateTime.now();

  factory Word.fromMap(Map<String, dynamic> map) {
    developer.log('Word.fromMap: 开始从Map创建Word对象: $map');
    try {
      final word = Word(
        id: map['id'] as int?,
        word: map['word'] as String,
        translation: map['translation'] as String?,
        pronunciation: map['pronunciation'] as String?, // 添加pronunciation字段映射
        meaning: map['meaning'] as String?, // 添加meaning字段映射
        example: map['example'] as String?,
        note: map['note'] as String?,
        category: map['category'] as String?,
        difficulty: (map['difficulty'] as num?)?.toInt() ?? 1,
        createdAt: map['createdAt'] != null
            ? (map['createdAt'] is DateTime
                ? map['createdAt'] as DateTime
                : DateTime.fromMillisecondsSinceEpoch(
                    (map['createdAt'] as num).toInt(),
                  ))
            : DateTime.now(),
        lastReviewedAt: map['lastReviewedAt'] != null
            ? (map['lastReviewedAt'] is DateTime
                ? map['lastReviewedAt'] as DateTime
                : DateTime.fromMillisecondsSinceEpoch(
                    (map['lastReviewedAt'] as num).toInt(),
                  ))
            : null,
        memoryStrength: (map['memoryStrength'] as num?)?.toInt() ?? 0,
      );
      developer.log('Word.fromMap: 成功创建Word对象: ${word.toString()}');
      return word;
    } catch (e) {
      developer.log('Word.fromMap: 创建Word对象失败: $e', error: e);
      rethrow;
    }
  }

  Map<String, dynamic> toMap() {
    developer.log('Word.toMap: 开始将Word对象转换为Map: $this');
    try {
      final map = {
        if (id != null) 'id': id,
        'word': word,
        if (translation != null) 'translation': translation,
        if (pronunciation != null) 'pronunciation': pronunciation, // 添加pronunciation字段映射
        if (meaning != null) 'meaning': meaning, // 添加meaning字段映射
        if (example != null) 'example': example,
        if (note != null) 'note': note,
        if (category != null) 'category': category,
        'difficulty': difficulty,
        'createdAt': createdAt.millisecondsSinceEpoch,
        if (lastReviewedAt != null)
          'lastReviewedAt': lastReviewedAt!.millisecondsSinceEpoch,
        'memoryStrength': memoryStrength,
      };
      developer.log('Word.toMap: 成功转换为Map: $map');
      return map;
    } catch (e) {
      developer.log('Word.toMap: 转换为Map失败: $e', error: e);
      rethrow;
    }
  }

  Word copyWith({
    int? id,
    String? word,
    String? translation,
    String? pronunciation, // 添加pronunciation参数
    String? meaning, // 添加meaning参数
    String? example,
    String? note,
    String? category,
    int? difficulty,
    DateTime? createdAt,
    DateTime? lastReviewedAt,
    int? memoryStrength,
  }) {
    return Word(
      id: id ?? this.id,
      word: word ?? this.word,
      translation: translation ?? this.translation,
      pronunciation: pronunciation ?? this.pronunciation, // 添加pronunciation字段复制
      meaning: meaning ?? this.meaning, // 添加meaning字段复制
      example: example ?? this.example,
      note: note ?? this.note,
      category: category ?? this.category,
      difficulty: difficulty ?? this.difficulty,
      createdAt: createdAt ?? this.createdAt,
      lastReviewedAt: lastReviewedAt ?? this.lastReviewedAt,
      memoryStrength: memoryStrength ?? this.memoryStrength,
    );
  }

  @override
  String toString() {
    return 'Word(id: $id, word: $word, pronunciation: $pronunciation, meaning: $meaning, example: $example, category: $category, difficulty: $difficulty, createdAt: $createdAt, lastReviewedAt: $lastReviewedAt, memoryStrength: $memoryStrength)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is Word &&
        other.id == id &&
        other.word == word &&
        other.pronunciation == pronunciation &&
        other.meaning == meaning &&
        other.example == example &&
        other.category == category &&
        other.difficulty == difficulty &&
        other.createdAt == createdAt &&
        other.lastReviewedAt == lastReviewedAt &&
        other.memoryStrength == memoryStrength;
  }

  @override
  int get hashCode {
    return id.hashCode ^
        word.hashCode ^
        pronunciation.hashCode ^
        meaning.hashCode ^
        example.hashCode ^
        category.hashCode ^
        difficulty.hashCode ^
        createdAt.hashCode ^
        lastReviewedAt.hashCode ^
        memoryStrength.hashCode;
  }
}